function callIt(refI){
	mrefs[refI].cycle();
}

var mrefs = [];
function Motion(element,origXY,newXY,origWH,newWH,inHowManyMilliseconds){
	mrefs.push(this);
	this.refIndex = mrefs.length-1;
	this.ctime = 10;
	this.time = Math.round(inHowManyMilliseconds/this.ctime);
	this.tcount = 0;
	this.element = element;
	this.origXY = [origXY[0],origXY[1]];
	this.newXY = [newXY[0],newXY[1]];
	this.origWH = [origWH[0],origWH[1]];
	this.newWH = [newWH[0],newWH[1]];
	this.changeXY = [(newXY[0]-origXY[0])/this.time,(newXY[1]-origXY[1])/this.time];
	this.changeWH = [(newWH[0]-origWH[0])/this.time,(newWH[1]-origWH[1])/this.time];
	this.currentXY = [origXY[0],origXY[1]];
	this.currentWH = [origWH[0],origWH[1]];
	this.cycle = function(){
	
		if ((this.changeXY[0] <= 0 && this.currentXY[0]+this.changeXY[0] <= this.newXY[0]) || 
			(this.changeXY[0] >= 0 && this.currentXY[0]+this.changeXY[0] >= this.newXY[0]))	{
			this.changeX(this.newXY[0]);
		}else this.changeX(this.currentXY[0]+this.changeXY[0]);
		
		if ((this.changeXY[1] <= 0 && this.currentXY[1]+this.changeXY[1] <= this.newXY[1]) || 
			(this.changeXY[1] >= 0 && this.currentXY[1]+this.changeXY[1] >= this.newXY[1]))	{
			this.changeY(this.newXY[1]);
		}else this.changeY(this.currentXY[1]+this.changeXY[1]);
		
		if ((this.changeWH[0] <= 0 && this.currentWH[0]+this.changeWH[0] <= this.newWH[0]) || 
			(this.changeWH[0] >= 0 && this.currentWH[0]+this.changeWH[0] >= this.newWH[0]))	{
			this.changeW(this.newWH[0]);
		}else this.changeW(this.currentWH[0]+this.changeWH[0]);
		
		if ((this.changeWH[1] <= 0 && this.currentWH[1]+this.changeWH[1] <= this.newWH[1]) || 
			(this.changeWH[1] >= 0 && this.currentWH[1]+this.changeWH[1] >= this.newWH[1]))	{
			this.changeH(this.newWH[1]);
		}else this.changeH(this.currentWH[1]+this.changeWH[1]);
	
		if (this.tcount >= this.time) return;
		this.tcount++;
		setTimeout("callIt("+this.refIndex+")",this.ctime);
	};
	
	this.changeX = function(x){
		this.currentXY[0] = x;
		this.element.style.left = x+"%";
	};
	
	this.changeY = function(y){
		this.currentXY[1] = y;
		this.element.style.top = y+"%";
	};
	
	this.changeW = function(w){
		this.currentWH[0] = w;
		this.element.style.width = w+"%";
	};
	
	this.changeH = function(h){
		this.currentWH[1] = h;
		this.element.style.height = h+"%";
	};
	
	this.cycle();
	return true;
}

function removeElement(_parent, _child) {
  var parent = document.getElementById(_parent);
  var child = document.getElementById(_child);
  parent.removeChild(child);
}

function getElementsByClassName(classname, node)  {
    if(!node) node = document.getElementsByTagName("body")[0];
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++){
		if(re.test(els[i].className)) a.push(els[i]);
	}
    return a;
}

var timeAdd_Take = 10;//option=1
var timeGamble = 15;//option=2
var timeGambleR4 = 25;//option=3
var timeDown = 30;//option=4
var timing=-1;
var t;
function startTimer(_elementId, _option){
	t=setTimeout("startTimer('"+_elementId+"', "+_option+")",1000);
	if(timing==-1){
		document.getElementById(_elementId).style.display = 'block';
		if(_option == 1) timing = timeAdd_Take;
		else if(_option == 2) timing = timeGamble;
		else if(_option == 3) timing = timeGambleR4;
		else if(_option == 4) timing = timeDown;
		document.getElementById(_elementId).appendChild(document.createTextNode(timing));
	}else{
		timing--;
		document.getElementById(_elementId).innerHTML=timing;
		if(timing == 0){
			autoGamble();
			stopTimer(_elementId); //goi ham can thiet
		}
	}
}

function stopTimer(_elementId){
	document.getElementById(_elementId).style.display = "none";
	clearTimeout(t);
	timing = -1;
}
